<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>showPage 函数测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background: #f5f5f5;
        }
        .test-container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            max-width: 600px;
            margin: 0 auto;
        }
        .test-item {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            padding: 15px;
            margin: 10px 0;
            border-radius: 4px;
        }
        .success {
            background: #e8f5e8;
            border-color: #4caf50;
        }
        .error {
            background: #ffebee;
            border-color: #f44336;
        }
        button {
            background: #1890ff;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #40a9ff;
        }
        .code {
            background: #263238;
            color: #ffffff;
            padding: 10px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="test-container">
        <h1>📋 showPage 函数测试</h1>
        <p>这个页面用于测试 showPage 函数是否正确定义和执行。</p>
        
        <div id="test-results">
            <h3>🔍 测试结果：</h3>
        </div>
        
        <h3>🎮 手动测试：</h3>
        <button onclick="testShowPageFunction()">测试 showPage 函数</button>
        <button onclick="testShowPageCall()">模拟页面切换</button>
        <button onclick="checkGlobalFunctions()">检查全局函数</button>
        
        <div id="manual-test-results"></div>
        
        <!-- 模拟页面结构 -->
        <div style="display: none;">
            <div id="dashboard" class="page">仪表板页面</div>
            <div id="stocks" class="page">股票页面</div>
            <div id="page-title">页面标题</div>
        </div>
    </div>

    <script>
        // 测试结果显示
        function addTestResult(title, status, message) {
            const results = document.getElementById('test-results');
            const className = status === 'success' ? 'test-item success' : 'test-item error';
            results.innerHTML += `<div class="${className}"><strong>${title}:</strong> ${message}</div>`;
        }
        
        // 检查 showPage 函数是否存在
        function testShowPageFunction() {
            const results = document.getElementById('manual-test-results');
            results.innerHTML = '<h4>🧪 函数测试结果:</h4>';
            
            // 检查函数是否定义
            if (typeof showPage === 'function') {
                results.innerHTML += '<div class="test-item success">✅ showPage 函数已定义</div>';
                
                // 尝试调用函数
                try {
                    showPage('dashboard');
                    results.innerHTML += '<div class="test-item success">✅ showPage 函数调用成功</div>';
                } catch (error) {
                    results.innerHTML += `<div class="test-item error">❌ showPage 函数调用失败: ${error.message}</div>`;
                }
            } else {
                results.innerHTML += '<div class="test-item error">❌ showPage 函数未定义</div>';
            }
        }
        
        // 模拟页面切换
        function testShowPageCall() {
            const results = document.getElementById('manual-test-results');
            results.innerHTML = '<h4>🔄 页面切换测试:</h4>';
            
            try {
                // 模拟点击元素
                const mockElement = document.createElement('a');
                mockElement.textContent = '测试链接';
                
                showPage('stocks', mockElement);
                results.innerHTML += '<div class="test-item success">✅ 页面切换测试成功</div>';
            } catch (error) {
                results.innerHTML += `<div class="test-item error">❌ 页面切换测试失败: ${error.message}</div>`;
            }
        }
        
        // 检查所有全局函数
        function checkGlobalFunctions() {
            const results = document.getElementById('manual-test-results');
            results.innerHTML = '<h4>🌐 全局函数检查:</h4>';
            
            const requiredFunctions = [
                'showPage', 'showAlert', 'loadStocksList', 'loadDashboardData',
                'loadPageData', 'generatePagination', 'changePageSize', 'jumpToPage'
            ];
            
            let allDefined = true;
            requiredFunctions.forEach(funcName => {
                if (typeof window[funcName] === 'function') {
                    results.innerHTML += `<div class="test-item success">✅ ${funcName} 已定义</div>`;
                } else {
                    results.innerHTML += `<div class="test-item error">❌ ${funcName} 未定义</div>`;
                    allDefined = false;
                }
            });
            
            if (allDefined) {
                results.innerHTML += '<div class="test-item success">🎉 所有必要函数都已正确定义！</div>';
            }
        }
        
        // 页面加载时自动运行基础测试
        window.addEventListener('load', function() {
            console.log('showPage 测试页面已加载');
            
            // 基础检查
            if (typeof showPage === 'function') {
                addTestResult('showPage 函数定义', 'success', '函数已正确定义');
            } else {
                addTestResult('showPage 函数定义', 'error', '函数未定义，可能有语法错误阻止了脚本执行');
            }
            
            // 检查其他关键函数
            const otherFunctions = ['showAlert', 'loadStocksList'];
            otherFunctions.forEach(funcName => {
                if (typeof window[funcName] === 'function') {
                    addTestResult(`${funcName} 函数`, 'success', '函数已定义');
                } else {
                    addTestResult(`${funcName} 函数`, 'error', '函数未定义');
                }
            });
        });
        
        // 捕获全局错误
        window.addEventListener('error', function(event) {
            addTestResult('JavaScript 错误', 'error', `${event.filename}:${event.lineno} - ${event.message}`);
        });
        
        window.addEventListener('unhandledrejection', function(event) {
            addTestResult('Promise 拒绝', 'error', event.reason.toString());
        });
    </script>
    
    <!-- 加载原始脚本 -->
    <script src="js/api.js"></script>
    <script src="js/main.js"></script>
</body>
</html>